//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BondFunctions.h"
using namespace Cephei::QL::Pricingengines::Bond;
#include <gen/QL/Instruments/Bond.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/InterestRate.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (boost::shared_ptr<QuantLib::BondFunctions>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBondFunctions = NULL;
#endif
	_ppBondFunctions = &childNative;
    
}
Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (QuantLib::BondFunctions& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBondFunctions = NULL;
#endif
	_ppBondFunctions = new boost::shared_ptr<QuantLib::BondFunctions> (&childNative);
    
    _BondFunctionsOwner = owner;
    
}

Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (CBondFunctions^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBondFunctions = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBondFunctions = new boost::shared_ptr<QuantLib::BondFunctions> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBondFunctions = NULL;
#endif
	if (!t->IsSubclassOf(CBondFunctions::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (QuantLib::Handle<QuantLib::BondFunctions>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBondFunctions = &childNative;
	_ppBondFunctions = &static_cast<boost::shared_ptr<QuantLib::BondFunctions>>(childNative.currentLink());
    
    _BondFunctionsOwner = owner;
}
Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (QuantLib::Handle<QuantLib::BondFunctions> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBondFunctions = &childNative;
	_ppBondFunctions = &static_cast<boost::shared_ptr<QuantLib::BondFunctions>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Bond::CBondFunctions::CBondFunctions (QuantLib::BondFunctions childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBondFunctions = NULL;
#endif
	_ppBondFunctions = new boost::shared_ptr<QuantLib::BondFunctions> (new QuantLib::BondFunctions (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Bond::CBondFunctions::~CBondFunctions ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBondFunctions != NULL)
    {
	    delete _ppBondFunctions;
        _ppBondFunctions = NULL;
    }
}
Cephei::QL::Pricingengines::Bond::CBondFunctions::!CBondFunctions ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBondFunctions != NULL)
    {
	    delete _ppBondFunctions;
    }
}
QuantLib::BondFunctions& Cephei::QL::Pricingengines::Bond::CBondFunctions::GetReference ()
{
    if (_ppBondFunctions == NULL) throw gcnew NativeNullException ();
	return **_ppBondFunctions;
}
boost::shared_ptr<QuantLib::BondFunctions>& Cephei::QL::Pricingengines::Bond::CBondFunctions::GetShared ()
{
    if (_ppBondFunctions == NULL) throw gcnew NativeNullException ();
	return *_ppBondFunctions;
}
QuantLib::BondFunctions* Cephei::QL::Pricingengines::Bond::CBondFunctions::GetPointer ()
{
    if (_ppBondFunctions == NULL) throw gcnew NativeNullException ();
	return &**_ppBondFunctions;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BondFunctions>& Cephei::QL::Pricingengines::Bond::CBondFunctions::GetHandle ()
{
	if (_phBondFunctions == NULL)
	{
		_phBondFunctions = new Handle<QuantLib::BondFunctions> (*_ppBondFunctions);
	}
	return *_phBondFunctions;
}
#endif
bool Cephei::QL::Pricingengines::Bond::CBondFunctions::HasNative () 
{
	return (_ppBondFunctions != NULL);
}

Double Cephei::QL::Pricingengines::Bond::CBondFunctions::AccruedAmount (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->accruedAmount ( _bond,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::AtmRate (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ cleanPrice)
{
    CBond^ _Cbond;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        QuantLib::YieldTermStructure& _discountCurve = static_cast<QuantLib::YieldTermStructure&> (_CdiscountCurve->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
        QuantLib::Real _cleanPrice = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (cleanPrice) ? (QuantLib::Real)ValueHelper::Convert (cleanPrice->Value) : Null<QuantLib::Real>()); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBondFunctions)->atmRate ( _bond,  _discountCurve,  _settlementDate,  _cleanPrice );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::BasisPointValue (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->basisPointValue ( _bond,  _yield,  _dayCounter,  _compounding,  _frequency,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::BasisPointValue (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CInterestRate^ _Cyield;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cyield = safe_cast<CInterestRate^> (yield);
        _Cyield->Lock();
        QuantLib::InterestRate& _yield = static_cast<QuantLib::InterestRate&> (_Cyield->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->basisPointValue ( _bond,  _yield,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cyield != nullptr) _Cyield->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Bps (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->bps ( _bond,  _yield,  _dayCounter,  _compounding,  _frequency,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Bps (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CInterestRate^ _Cyield;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cyield = safe_cast<CInterestRate^> (yield);
        _Cyield->Lock();
        QuantLib::InterestRate& _yield = static_cast<QuantLib::InterestRate&> (_Cyield->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->bps ( _bond,  _yield,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cyield != nullptr) _Cyield->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Bps (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        QuantLib::YieldTermStructure& _discountCurve = static_cast<QuantLib::YieldTermStructure&> (_CdiscountCurve->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->bps ( _bond,  _discountCurve,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::CleanPrice (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discount, Double zSpread, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CYieldTermStructure^ _Cdiscount;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cdiscount = safe_cast<CYieldTermStructure^> (discount);
        _Cdiscount->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _discount = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_Cdiscount->GetShared ()); 
        QuantLib::Spread _zSpread = (QuantLib::Spread)ValueHelper::Convert (zSpread);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->cleanPrice ( _bond,  _discount,  _zSpread,  _dayCounter,  _compounding,  _frequency,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cdiscount != nullptr) _Cdiscount->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::CleanPrice (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->cleanPrice ( _bond,  _yield,  _dayCounter,  _compounding,  _frequency,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::CleanPrice (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CInterestRate^ _Cyield;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cyield = safe_cast<CInterestRate^> (yield);
        _Cyield->Lock();
        QuantLib::InterestRate& _yield = static_cast<QuantLib::InterestRate&> (_Cyield->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->cleanPrice ( _bond,  _yield,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cyield != nullptr) _Cyield->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::CleanPrice (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        QuantLib::YieldTermStructure& _discountCurve = static_cast<QuantLib::YieldTermStructure&> (_CdiscountCurve->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->cleanPrice ( _bond,  _discountCurve,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Convexity (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->convexity ( _bond,  _yield,  _dayCounter,  _compounding,  _frequency,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Convexity (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CInterestRate^ _Cyield;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cyield = safe_cast<CInterestRate^> (yield);
        _Cyield->Lock();
        QuantLib::InterestRate& _yield = static_cast<QuantLib::InterestRate&> (_Cyield->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->convexity ( _bond,  _yield,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cyield != nullptr) _Cyield->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Duration (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Duration::TypeEnum>^ type, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Duration::Type _type = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Duration::TypeEnum>::IsSome::get (type) ? (QuantLib::Duration::Type)type->Value : QuantLib::Duration::Modified ); //5
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppBondFunctions)->duration ( _bond,  _yield,  _dayCounter,  _compounding,  _frequency,  _type,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Duration (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Duration::TypeEnum>^ type, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CInterestRate^ _Cyield;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cyield = safe_cast<CInterestRate^> (yield);
        _Cyield->Lock();
        QuantLib::InterestRate& _yield = static_cast<QuantLib::InterestRate&> (_Cyield->GetReference ()); 
        QuantLib::Duration::Type _type = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Duration::TypeEnum>::IsSome::get (type) ? (QuantLib::Duration::Type)type->Value : QuantLib::Duration::Modified ); //5
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppBondFunctions)->duration ( _bond,  _yield,  _type,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cyield != nullptr) _Cyield->Unlock();
    }
}
Boolean Cephei::QL::Pricingengines::Bond::CBondFunctions::IsTradable (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	bool _rv = (bool)(*_ppBondFunctions)->isTradable ( _bond,  _settlementDate );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
DateTime Cephei::QL::Pricingengines::Bond::CBondFunctions::MaturityDate (Cephei::QL::Instruments::IBond^ bond)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBondFunctions)->maturityDate ( _bond );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::NextCashFlowAmount (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _refDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refDate) ? (QuantLib::Date)ValueHelper::Convert (refDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->nextCashFlowAmount ( _bond,  _refDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
DateTime Cephei::QL::Pricingengines::Bond::CBondFunctions::NextCashFlowDate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _refDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refDate) ? (QuantLib::Date)ValueHelper::Convert (refDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBondFunctions)->nextCashFlowDate ( _bond,  _refDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::NextCouponRate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBondFunctions)->nextCouponRate ( _bond,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::PreviousCashFlowAmount (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _refDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refDate) ? (QuantLib::Date)ValueHelper::Convert (refDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->previousCashFlowAmount ( _bond,  _refDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
DateTime Cephei::QL::Pricingengines::Bond::CBondFunctions::PreviousCashFlowDate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _refDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refDate) ? (QuantLib::Date)ValueHelper::Convert (refDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBondFunctions)->previousCashFlowDate ( _bond,  _refDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::PreviousCouponRate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBondFunctions)->previousCouponRate ( _bond,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
DateTime Cephei::QL::Pricingengines::Bond::CBondFunctions::StartDate (Cephei::QL::Instruments::IBond^ bond)
{
    CBond^ _Cbond;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBondFunctions)->startDate ( _bond );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::Yield (Cephei::QL::Instruments::IBond^ bond, Double cleanPrice, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Real _cleanPrice = (QuantLib::Real)ValueHelper::Convert (cleanPrice);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-10); //9a
        QuantLib::Size _maxIterations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxIterations) ? (QuantLib::Size)ValueHelper::Convert (maxIterations->Value) : 100); //9a
        QuantLib::Rate _guess = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (guess) ? (QuantLib::Rate)ValueHelper::Convert (guess->Value) : 0.05); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBondFunctions)->yield ( _bond,  _cleanPrice,  _dayCounter,  _compounding,  _frequency,  _settlementDate,  _accuracy,  _maxIterations,  _guess );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::YieldValueBasisPoint (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Rate _yield = (QuantLib::Rate)ValueHelper::Convert (yield);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->yieldValueBasisPoint ( _bond,  _yield,  _dayCounter,  _compounding,  _frequency,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::YieldValueBasisPoint (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate)
{
    CBond^ _Cbond;
    CInterestRate^ _Cyield;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        _Cyield = safe_cast<CInterestRate^> (yield);
        _Cyield->Lock();
        QuantLib::InterestRate& _yield = static_cast<QuantLib::InterestRate&> (_Cyield->GetReference ()); 
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondFunctions)->yieldValueBasisPoint ( _bond,  _yield,  _settlementDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cyield != nullptr) _Cyield->Unlock();
    }
}
Double Cephei::QL::Pricingengines::Bond::CBondFunctions::ZSpread (Cephei::QL::Instruments::IBond^ bond, Double cleanPrice, Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess)
{
    CBond^ _Cbond;
    CYieldTermStructure^ _Cprm1;
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        QuantLib::Bond& _bond = static_cast<QuantLib::Bond&> (_Cbond->GetReference ()); 
        QuantLib::Real _cleanPrice = (QuantLib::Real)ValueHelper::Convert (cleanPrice);
        _Cprm1 = safe_cast<CYieldTermStructure^> (prm1);
        _Cprm1->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _prm1 = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_Cprm1->GetShared ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Compounding _compounding = (QuantLib::Compounding)compounding ;
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::Date _settlementDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (settlementDate) ? (QuantLib::Date)ValueHelper::Convert (settlementDate->Value) : QuantLib::Date()); //9a
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-10); //9a
        QuantLib::Size _maxIterations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxIterations) ? (QuantLib::Size)ValueHelper::Convert (maxIterations->Value) : 100); //9a
        QuantLib::Rate _guess = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (guess) ? (QuantLib::Rate)ValueHelper::Convert (guess->Value) : 0.0); //9a
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppBondFunctions)->zSpread ( _bond,  _cleanPrice,  _prm1,  _dayCounter,  _compounding,  _frequency,  _settlementDate,  _accuracy,  _maxIterations,  _guess );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbond != nullptr) _Cbond->Unlock();
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

